package me.project.jobster.controller;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.BeanUtils;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Student;
import me.project.jobster.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author mbryce
 * @date 2018/5/1
 */
@Controller
@RequestMapping("company")
public class CompanyController extends BaseController {


    @Autowired
    private UserService userService;

    /**
     * stusents page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("index")
    public String index(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "2");
        List<Company> list = userService.getCompanyList();

        model.addAttribute("companyList", list);
        model.addAttribute("company", new Company());
        return "company/companies";
    }

    /**
     * search page
     * @param request
     * @param model
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @PostMapping("search")
    public String search(HttpServletRequest request, Model model) throws InvocationTargetException, IllegalAccessException {
        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "2");

        Company company = new Company();
        BeanUtils.copyMap2Bean(company, getParameterMap(request));
        List<Company> list = userService.getCompanyListFuzzy(company);
        model.addAttribute("companyList", list);
        model.addAttribute("company", company);
        return "company/companies";
    }

    /**
     * student detail page
     * @param request
     * @param model
     * @return
     */
    @GetMapping("detail")
    public String detail(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "2");
        String userId = request.getParameter("cId");

        Company company = new Company();
        company.setcId(userId);
        Company c = userService.getCompanyInfo(userId);
        model.addAttribute("company", c);
        return "company/detail";
    }

}
